package com.chinawlx.wlxteacher.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.ui.adapter.WLXStudentRollCallAdapter;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;
import com.chinawlx.wlxteacher.wlx_student;
import com.chinawlx.wlxteacher.wlx_studentDao;
import com.chinawlx.wlxteacher.wlx_student_order_detail;
import com.chinawlx.wlxteacher.wlx_student_order_detailDao;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 课堂点名界面
 * <p/>
 * Created by wlx on 16/8/18.
 */
public class WLXCourseRollCallActivity extends WLXBaseActivity {
    @BindView(R.id.recylerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_attendance)
    TextView attendance;
    @BindView(R.id.tv_leave)
    TextView leave;
    @BindView(R.id.tv_truant)
    TextView truant;
    @BindView(R.id.tv_confirm_callroll)
    TextView confirm;
    @BindView(R.id.iv_back_course_call_roll)
    ImageView backBtn;
    private PopupWindow mPopupWindow = null;
    private ArrayList<WLXCourseRollCallActivity.StudentInfoBean> wlxStudents = new ArrayList<>();
    private String gradeId;
    private String scheduleId;
    private String studentIds;
    private int isAddition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_call_roll);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initData();
        initView();
    }

    private void initView() {
        //设置布局管理器
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        //设置适配器
        WLXStudentRollCallAdapter rollCallAdapter = new WLXStudentRollCallAdapter(this, wlxStudents,
                attendance, confirm,leave,truant,gradeId,scheduleId);
        mRecyclerView.setAdapter(rollCallAdapter);
        //添加分割线
//        mRecyclerView.addItemDecoration();
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        gradeId = intent.getStringExtra(WLXTeacherConstant.GRADE_ID);
        scheduleId = intent.getStringExtra(WLXTeacherConstant.SCHEDULE_ID);
        studentIds = intent.getStringExtra(WLXTeacherConstant.STUDENT_IDS);
        isAddition = intent.getIntExtra(WLXTeacherConstant.IS_ADDITION,0);
        List<wlx_student_order_detail> wlxStudentOrderDetails = WLXGreenDaoUtil
                .getStudentOrderDetailDao()
                .queryBuilder().where
                        (wlx_student_order_detailDao.Properties.Grade_id.eq(gradeId)).list();
        for (wlx_student_order_detail detail : wlxStudentOrderDetails) {
            WLXCourseRollCallActivity.StudentInfoBean bean = new WLXCourseRollCallActivity.StudentInfoBean();
            bean.setNum(detail.getBalance_number());
            bean.setStudent_id(detail.getStudent_id());
            List<wlx_student> students = WLXGreenDaoUtil.getStudentDao().queryBuilder().where
                    (wlx_studentDao.Properties.Student_id.eq(detail.getStudent_id())).list();
            wlx_student student = students.get(0);
            bean.setName(student.getRealname() + "");
            bean.setIcon(student.getAvatar_url() + "");

            wlxStudents.add(bean);
        }
    }


    //学生信息bean,保存查询到的学生信息
    public class StudentInfoBean {
        int student_id;
        String icon;
        String name;
        int num;

        public int getStudent_id() {
            return student_id;
        }

        public void setStudent_id(int student_id) {
            this.student_id = student_id;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}
